﻿<div class="layui-card" id="lg-AddApply">
    <div class="layui-card-header">
        <i class="fa fa-home fa-fw"></i>添加学生
        <div class="f-r">
            <button class="layui-btn layui-btn-primary layui-btn-sm" v-on:click="back()"><i class="fa fa-mail-reply fa-fw"></i>返回</button>
        </div>
    </div>
    <div class="layui-card-body" id="">
        <div class="layui-form-item">
            <template v-if="plan!=null">
                <div class="lg-form-head">
                    <div class="lg-form-head-title">
                        <input class="lg-input lg-input-text" placeholder="表单标题" v-model="plan.Title" readonly>
                    </div>
                    <div class="form-head-intro" v-html="plan.PlanTips">
                    </div>
                </div>
                <div class="form-body">
                    <lg-form :data="form" v-on:submit="submit" bt-text="立即申请" v-on:done="done"></lg-form>
                </div>
            </template>
        </div>
        <!-- 内容主体区域结束 -->
    </div>
</div>

<script type="module">
    import { freeForm } from '/plugins/lgform/lg.form.2.1.js'
    freeForm.addEventListener();
    frame.createPage({
        mounted: function() {
            var page = this;
            var app = Vue.createApp({
                data() {
                    return {
                        plan: null,
                        form: []
                    }
                },
                mounted: function() {
                    this.getPlan();
                },
                methods: {
                    back: function() {
                        frame.Back(page.url, res => {
                            res.laytable.reload()
                        })
                    },
                    getPlan: function() {
                        lg.request({
                            url: "/api/sys/apply/getPlan/?id=" + @ViewBag.id,
                            success: res => {
                                if (res.code == 0) {
                                    if (res.data != null) {
                                        this.plan = res.data;
                                        var items = freeForm.toJson(res.data.FormItems, []);
                                        this.form = items;
                                    }
                                } else {
                                    layer.msg(res.msg, {
                                        icon: 2
                                    })
                                }
                            }
                        })
                    },
                    GetComponent: function(key) {
                        return freeForm.types[key].component.name;
                    },
                    submit: function(data) {
                        var that = this;
                        var errCount = 0;
                        var obj = {};
                        var items = this.form
                        //for (let i in items) {
                        //    let item = items[i];
                        //    if (item.Data.IsRender) {
                        //        var res = diyForm.verify(item.TypeName, item.Data)
                        //        if (!res.state) {
                        //            item.Data.error = true;
                        //            item.Data.errMsg = res.msg;
                        //            errCount++;
                        //        } else {
                        //            item.Data.error = false;
                        //            item.Data.errMsg = "验证通过！";
                        //        }
                        //    }
                        //}
                        if (errCount == 0) {
                            for (let i in items) {
                                var type = items[i].Data;
                                if (type.IsRender) {
                                    if (type.getValue) {
                                        obj[type.Name] = type.getValue();
                                    } else {
                                        obj[type.Name] = type.Value;
                                    }
                                } else {
                                    obj[type.Name] = 'lg-no-render';
                                }
                            }
                            //身份证和出生日期联合较验
                            //if (obj.IdType.indexOf("身份证") > -1) {
                            //    if (typeof obj.IdNumber == "object") {
                            //        var b = lg.age.getBirthday(obj.IdNumber.Value) == obj.Birthday;
                            //        if (!b) {
                            //            layer.msg("您填写的出生日期与身份证上的日期不一致！", { icon: 2 });
                            //            return false;
                            //        }
                            //    }
                            //}
                            lg.request({
                                url: "/api/sys/apply/AddApply",
                                data: { PlanId: this.plan.Id, FormData: JSON.stringify(obj) },
                                method: "post",
                                success: res => {
                                    if (res.code == 0) {
                                        layer.msg("报名成功！", { icon: 1, time: 1000 }, function() {
                                            that.back();
                                        })
                                    } else {
                                        layer.msg(res.msg, { icon: 2 })
                                    }
                                }
                            })
                        } else {
                            layer.msg("表单较验未通过，请修改后再行提交！", { icon: 2 })
                        }
                    },
                    done: function(item) {
                        if (item.Name == "IdNumber" && item.IdType.indexOf("身份证") > -1) {
                            if (lg.regx.idCard(item.Value)) {
                                var arry = this.form.filter(p => p.Data.Name == "Birthday");
                                if (arry.length > 0) {
                                    arry[0].Data.Value = lg.age.getBirthday(item.Value);
                                }
                            }
                        }
                    }
                }
            })
            freeForm.regComponent(app);
            app.mount("#lg-AddApply")
        }
    }).mount();
</script>